package actions;

import java.sql.SQLException;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.sql.rowset.CachedRowSet;

import model.Database;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;

public class ByReviewerAction extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws DbException {
			HttpSession session = request.getSession();
		
				try {
					CachedRowSet gradedSet = (CachedRowSet)Database.getAllGraded();
					CachedRowSet notGradedSet = (CachedRowSet)Database.getAllNotGraded();
					CachedRowSet reviewersSet = (CachedRowSet) Database.getAllReviewers();
					Vector<String> reviewers = new Vector<String>();
					Vector<Integer> reviewersId = new Vector<Integer>();
					Vector<Integer> graded = new Vector<Integer>();
					Vector<Integer> notGraded = new Vector<Integer>();
					
					
					
					while(reviewersSet.next()){
						CachedRowSet reviewerGraded = Database.getGradedByReviewer(reviewersSet.getInt("user_ref"));
						CachedRowSet reviewerNotGraded = Database.getNotGradedByReviewer(reviewersSet.getInt("user_ref"));
						reviewerGraded.next();
						reviewerNotGraded.next();
						
						reviewers.add(reviewersSet.getString("user_name"));
						reviewersId.add(reviewersSet.getInt("user_ref"));
						
						//add graded ones
						try{reviewerGraded.getInt(1);
						graded.add(reviewerGraded.size());
						}catch(ArrayIndexOutOfBoundsException e){
							graded.add(0);
						}
						
						//add non graded ones
						try{reviewerNotGraded.getInt(1);
						notGraded.add(reviewerNotGraded.size());
						}catch(ArrayIndexOutOfBoundsException e){
							notGraded.add(0);
						}
						
					}
					
					session.setAttribute("graded", graded);
					session.setAttribute("notGraded", notGraded);
					session.setAttribute("reviewers", reviewers);
					session.setAttribute("reviewersId", reviewersId);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				return mapping.findForward("success");
		
	}

}
